package org.ghost.http.testkit.controller;

import com.baomidou.mybatisplus.plugins.Page;
import groovy.lang.Tuple2;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.ghost.http.testkit.dto.ConfigTestDataDTO;
import org.ghost.http.testkit.dto.ResponseVO;
import org.ghost.http.testkit.entity.ConfigTestData;
import org.ghost.http.testkit.service.IConfigTestDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2018-05-02
 */
@RestController
@RequestMapping("/configTestData")
public class ConfigTestDataController extends BaseController {
    @Autowired
    private IConfigTestDataService service;

    /**
     * 实体查询
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResponseVO search(@PathVariable Long id) {
        return getFromData(service.selectById(id));
    }

    /**
     * 实体删除
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResponseVO del(@PathVariable Long id) {
        service.deleteById(id);
        return getSuccess();
    }

    /**
     * 实体添加
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseVO add(@RequestBody ConfigTestData entity) {
        service.insert(entity);
        return getSuccess();
    }


    /**
     * 实体修改
     */
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResponseVO update(@PathVariable Long id, @RequestBody ConfigTestData entity) {
        entity.setId(id);
        service.updateById(entity);
        return getSuccess();
    }

    /**
     * 实体添加
     */
    @RequestMapping(value = "addOrUpdate", method = RequestMethod.POST)
    public ResponseVO addEntity(ConfigTestData entity) {
        if (entity != null) {
            if (entity.getId() != null && entity.getId() == 0L) {
                entity.setId(null);
            }
            if (entity.getIsDel() == null) {
                entity.setIsDel(0);
            }
            if (entity.getCreatorId() == null) {
                entity.setCreatorId(1L);
            }
            if (entity.getModifierId() == null) {
                entity.setModifierId(1L);
            }
            if (entity.getCreatedAt() == null) {
                entity.setCreatedAt(new Date());
            }
            if (entity.getModifiedAt() == null) {
                entity.setModifiedAt(new Date());
            }

            if (entity.getId() == null) {
                service.insert(entity);
            } else {
                service.updateById(entity);
            }
            return getSuccess();
        }

        return getFailure();
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseVO selectPage(Page<ConfigTestData> page) {
        page = page == null ? new Page<>(1, 10) : page;
        ConfigTestDataDTO reqDTO = new ConfigTestDataDTO();
        if (MapUtils.isNotEmpty(page.getCondition())) {
            reqDTO.setEnvId(MapUtils.getLong(page.getCondition(), "envId"));
            reqDTO.setSysId(MapUtils.getLong(page.getCondition(), "sysId"));
            reqDTO.setInterfaceName(MapUtils.getString(page.getCondition(), "interfaceName"));
        }

        List<ConfigTestDataDTO> result = service.selectConfigTestData(page, reqDTO);
        if (CollectionUtils.isNotEmpty(result)) {
            Page<ConfigTestDataDTO> pageResult = new Page<ConfigTestDataDTO>(page.getCurrent(), page.getSize());
            pageResult.setTotal(page.getTotal());
            pageResult.setRecords(result);

            return getFromData(pageResult);
        }

        return getFromData(page);
    }

    @RequestMapping(value = "/test/{id}", method = RequestMethod.GET)
    public ResponseVO testById(@PathVariable Long id) {
        ConfigTestData configTestData = service.selectById(id);
        if (configTestData != null) {
            Tuple2<Boolean, String> result = service.test(configTestData.getInterfaceId(), configTestData.getEnvId());
            String data = result.getFirst() + "->" + result.getSecond();
            return getFromData(data);
        } else {
            return getFailure();
        }
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ResponseVO test(@RequestParam Long envId, @RequestParam Long interfaceId) {
        Tuple2<Boolean, String> result = service.test(interfaceId, envId);
        String data = result.getFirst() + "->" + result.getSecond();
        return getFromData(data);
    }

    @RequestMapping(value = "/testAll", method = RequestMethod.GET)
    public ResponseVO testAll(@RequestParam Long envId, @RequestParam Long sysId) {
        Map<Long, Tuple2<Boolean, String>> result = service.testAll(envId, sysId);
        if (MapUtils.isNotEmpty(result)) {
            String data = result.entrySet().stream().map(it -> it.getKey() + "->" + it.getValue().getFirst() + "," + it.getValue().getSecond()).reduce((a, b) -> {
                if (StringUtils.isNotBlank(a)) {
                    return a + "," + b;
                } else {
                    return b;
                }
            }).orElse(null);

            return getFromData(data);
        }

        return getFromData(null);
    }
}
